using Mathmall.Administration.Data;
using Mathmall.Administration.Models;
using Mathmall.Administration.Vo;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;

namespace Mathmall.Administration.Mapper;

public class AttrMapper : BaseMapper
{
    public AttrMapper(AdministrationContext context) : base(context)
    {
    }

    /// <summary>
    /// 保存属性信息
    /// </summary>
    /// <param name="attr"></param>
    /// <returns></returns>
    public async Task<int> AddAttr(Attr attr)
    {
        _context.Attrs.Add(attr);
        await _context.SaveChangesAsync();
        return attr.AttrID;
    }

    /// <summary>
    /// 查询全部属性
    /// </summary>
    /// <returns></returns>
    public IQueryable<Attr> GetAllAttrs()
    {
        return _context.Attrs;
    }

    /// <summary>
    /// 根据categoryId查询属性信息
    /// </summary>
    /// <param name="catalogId">分类id</param>
    /// <returns></returns>
    public async Task<List<Attr>?> GetAttrByCategoryId(int catalogId)
    {
        return await _context.Attrs.AsNoTracking().Where(attr => catalogId.Equals(attr.CatalogID)).ToListAsync();
    }

    /// <summary>
    /// 修改属性值
    /// </summary>
    /// <param name="attr"></param>
    /// <returns></returns>
    public async Task<int> UpdateAttr(Attr attr)
    {
        _context.Attrs.Update(attr);
        return await _context.SaveChangesAsync();
    }

    /// <summary>
    /// 删除属性
    /// </summary>
    /// <param name="attrIds"></param>
    /// <returns></returns>
    public async Task<int> DeleteAttr(List<int> attrIds)
    {
        return await _context.Attrs.Where(attr => attrIds.Contains(attr.AttrID)).ExecuteDeleteAsync();
    }
}